package com.drinks.yiniang.adapter.home;

import android.content.Context;
import android.support.annotation.Nullable;
import android.view.View;

import com.chad.library.adapter.base.BaseViewHolder;
import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.base.BaseMultiItemQuickRCVAdapter;
import com.drinks.yiniang.entity.goods.GoodsJson;
import com.drinks.yiniang.entity.goods.HomeGoodsJson;
import com.drinks.yiniang.ui.widget.textview.TagTextView;
import com.drinks.yiniang.utils.GlideUtils;
import com.hansen.library.ui.widget.image.RatioRoundImageView;
import com.hansen.library.ui.widget.layout.AutoLineFeedLayout;
import com.hansen.library.utils.CommonUtils;
import com.hansen.library.utils.StringUtils;

import java.util.List;

import static com.drinks.yiniang.entity.goods.HomeGoodsJson.ITEM_TYPE_HOME_AD_LIST;
import static com.drinks.yiniang.entity.goods.HomeGoodsJson.ITEM_TYPE_HOME_GOODS_LIST;

/**
 * 首页商品数据adapter
 *
 * @author han
 * @date 2019/8/30
 */
public class HomePageAdapter extends BaseMultiItemQuickRCVAdapter<HomeGoodsJson.HomeGoodsList, BaseViewHolder> {

    private TagTextView mTagTextView;

    private StringBuilder mBuilder;

    public HomePageAdapter(Context context, @Nullable List<HomeGoodsJson.HomeGoodsList> data) {
        super(data);

        this.mContext = context;

        addItemType(ITEM_TYPE_HOME_GOODS_LIST, R.layout.item_home_goods);
        addItemType(ITEM_TYPE_HOME_AD_LIST, R.layout.item_home_ad);
    }

    @Override
    protected void convert(BaseViewHolder helper, HomeGoodsJson.HomeGoodsList item) {
        switch (helper.getItemViewType()) {//商品列表
            case ITEM_TYPE_HOME_GOODS_LIST:
                GlideUtils.loadGoodsList(mContext, (RatioRoundImageView) helper.getView(R.id.iv_home_goods_cover), item.getCover_url());

                //活动tag
                addLabelTag((AutoLineFeedLayout) helper.getView(R.id.ll_home_goods_label), item.getActivity());

                //商品名称
                helper.setText(R.id.tv_home_goods_name, item.getName());
                //推荐语
                helper.setText(R.id.tv_home_goods_slogan, item.getRecommend_reason());
                //价格
                mBuilder = new StringBuilder();
                mBuilder.append("¥");
                mBuilder.append(StringUtils.getGoodsPrice(item.getApp_price(), item.getActivity_price()));
                helper.setText(R.id.tv_home_goods_price, mBuilder);

                helper.addOnClickListener(R.id.iv_home_goods_add_cart);
                break;
            case ITEM_TYPE_HOME_AD_LIST://商品广告列表
                GlideUtils.loadGoodsList(mContext, (RatioRoundImageView) helper.getView(R.id.iv_home_ad_cover), item.getCover_url());

                //标题
                helper.setText(R.id.tv_home_ad_title, item.getTitle());
                helper.setText(R.id.tv_home_ad_slogan, item.getSub_title());
                break;
            default:
                break;
        }
    }

    private void addLabelTag(AutoLineFeedLayout layout, List<GoodsJson.GoodsActivity> actList) {
        layout.removeAllViews();

        if (CommonUtils.isEmptyList(actList)) {
            layout.setVisibility(View.GONE);

            return;
        }

        layout.setVisibility(View.VISIBLE);

        for (GoodsJson.GoodsActivity data : actList) {
            mTagTextView = new TagTextView(mContext);

            mTagTextView.setTagColor(data.getTag_color());
            mTagTextView.setText(data.getTag_name());

            layout.addView(mTagTextView);
        }
    }
}
